<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <div>
      <el-button size="small" type="primary" @click="toAddModel()">新增</el-button>
    </div>
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partAccessDesignIndex"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="saveData"
    >
      <template slot="moreActions">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="deleteRow()">删除</el-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visible" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '70px' }">
      <BrandModal :type="handleType" :rowData="formData" @close="closeHandler" />
    </Drawer>
    <Drawer :visible.sync="visible1" :title="dialogTitle1" destroyOnClose1 :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '70px' }">
      <addModel :type="handleType1" :rowData="formData1" @close="closeHandler1" />
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { partStocktakingByDto, deleteBrandByStockNo } from '@/api/parts/access/design';
import BrandModal from './tabs/brandModal';
import addModel from './tabs/addModel';

export default {
  name: 'Brand',
  components: {
    BrandModal,
    addModel
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    this.BaseTable = null;
    return {
      arr: [],
      topFilterList: this.createTopFilterList(),
      fetchParams: {},
      visible: false,
      visible1: false,
      dialogTitle: '',
      dialogTitle1: '',
      fetchApi: partStocktakingByDto,
      columns: this.createTableColumns(),
      handleType: '',
      handleType1: '配件盘点计划新增',
      formData: {},
      formData1: {}
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '盘点计划编码',
          fieldName: 'stockTakingNo',
          placeholder: '盘点计划编码'
        },
        {
          type: 'INPUT',
          label: '盘点计划名称',
          fieldName: 'stockTakingName',
          placeholder: '盘点计划名称'
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'planBegin|planEnd',
          placeholder: '创建日期'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 60,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '仓库',
          dataIndex: 'stockTakingCycle',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '盘点计划编码',
          dataIndex: 'stockTakingNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '盘点计划名称',
          dataIndex: 'stockTakingName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '计划周期起始日期',
          dataIndex: 'planBegin',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '计划周期结束日期',
          dataIndex: 'planEnd',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '更新日期',
          dataIndex: 'updatedAt',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    toAddModel() {
      this.visible1 = true;
      this.handleType1 = 'add';
      this.showHandler1();
    },
    editHandle(row) {
      console.log(row);
      this.formData = row;
      this.handleType = 'edit';
      this.visible = true;

      this.showHandler();
    },
    saveData(row) {
      if (row.length > 0) {
        for (let i = 0; i < row.length; i++) {
          this.arr.push(row[i].stockTakingNo);
        }
      }
      console.log(this.arr);
    },
    async deleteRow() {
      console.log(this.arr);
      let res = null;
      res = await deleteBrandByStockNo({
        stockTakingNo: this.arr
      });
      console.log(res, '1');
      this.BaseTable.EXECUTE_DELETE(this.arr);
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    showHandler() {
      this.dialogTitle = `${this.conf[this.handleType]}品牌`;
      this.visible = true;
    },
    closeHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
    showHandler1() {
      this.dialogTitle1 = `${this.conf[this.handleType1]}品牌`;
      this.visible1 = true;
    },
    closeHandler1(val, type) {
      this.visible1 = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams1 = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
